import random
import requests
from json_repair import repair_json
import json
def hanguangAgent(content,apikey):
    header={
    'Authorization': 'Bearer {}'.format(apikey),
    'Content-Type': 'application/json'
    }
    data={
        "chatId": "{}".format(random.randint(0,1111111)),
        "stream": False,
        "detail": False,
        "messages": [
            {
                "content": "{}".format(content),
                "role": "user"
            }
        ]
    }
    resp = requests.post('https://model.zscampus.com/api/v1/chat/completions',data=json.dumps(data),headers=header)
    try:
        result = resp.json()['choices'][0]['message']['content']
        
        result = result.replace('\n','')
        startIdx = result.index('{')
        endIdx = result.rindex('}')+1
        result =result[startIdx:endIdx].replace('\n','')
        result = repair_json(result)
        result = json.loads(result)
        print(result,type(result))
    except Exception as e:
        print(e)
        result=''
    return result

def getExamAna(examResult):
    c= '我的测评量表填写结果是:\n\n{}'.format(examResult)
    print(c)
    ak = 'fastgpt-HOfEF8zu7NMZSNxlQHx18VCn'
    examContent = hanguangAgent(c,ak)
    print(examContent)
    # rs = {"code": 200, "data": [], "msg": "成功"}
    # data = examContent
    # rs["data"] = examContent
    return examContent